using System;
using System.Collections.Generic;
using System.Windows.Media;
namespace Microsoft.Phone.Controls.Maps.Core
{
	internal sealed class MultiScaleQuadTileSource : MultiScaleTileSource
	{
		private struct TileInfo
		{
			public int X
			{
				get;
				set;
			}
			public int Y
			{
				get;
				set;
			}
			public int Layer
			{
				get;
				set;
			}
		}
		private const int minZoomLevel = 1;
		private const int maxZoomLevel = 22;
		private const int numberOfEdgeTilesAtMaxZoomLevel = 4194304;
		private readonly List<MultiScaleQuadTileSource.TileInfo>[] fakeNoTile;
		private readonly TileSourceCollection tileSources;
		private readonly int zoomLevelDelta;
		private int minTileZoomLevel;
		public TileSourceCollection TileSources
		{
			get
			{
				return this.tileSources;
			}
		}
		public int MinTileZoomLevel
		{
			get
			{
				return this.minTileZoomLevel;
			}
			set
			{
				int num = this.minTileZoomLevel;
				this.minTileZoomLevel = value;
				if (this.minTileZoomLevel < num)
				{
					this.RefreshInvalidTiles();
				}
			}
		}
		public MultiScaleQuadTileSource(TileSource tileSource, int tileWidth, int tileHeight) : this(tileWidth, tileHeight)
		{
			if (tileSource != null)
			{
				this.tileSources.Add(tileSource);
			}
		}
		public MultiScaleQuadTileSource(TileSourceCollection tileSources, int tileWidth, int tileHeight) : this(tileWidth, tileHeight)
		{
			if (tileSources != null)
			{
				using (IEnumerator<TileSource> enumerator = tileSources.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						TileSource current = enumerator.get_Current();
						this.tileSources.Add(current);
					}
				}
			}
		}
		private MultiScaleQuadTileSource(int tileWidth, int tileHeight) : base(4194304 * tileWidth, 4194304 * tileHeight, tileWidth, tileHeight, 0)
		{
			this.tileSources = new TileSourceCollection();
			this.zoomLevelDelta = (int)Math.Log((double)tileWidth, 2.0);
			base.set_TileBlendTime(new TimeSpan(0, 0, 0, 0, 250));
			this.fakeNoTile = new List<MultiScaleQuadTileSource.TileInfo>[23];
			for (int i = 0; i < this.fakeNoTile.Length; i++)
			{
				this.fakeNoTile[i] = new List<MultiScaleQuadTileSource.TileInfo>();
			}
		}
		protected override void GetTileLayers(int tileLevel, int tilePositionX, int tilePositionY, IList<object> tileImageLayerSources)
		{
			int num = tileLevel - this.zoomLevelDelta;
			int num2 = this.MinTileZoomLevel;
			if (num >= 1 && num <= 22)
			{
				int num3 = 0;
				using (IEnumerator<TileSource> enumerator = this.tileSources.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						TileSource current = enumerator.get_Current();
						Uri uri;
						if (num < num2)
						{
							uri = null;
							this.fakeNoTile[num].Add(new MultiScaleQuadTileSource.TileInfo
							{
								X = tilePositionX,
								Y = tilePositionY,
								Layer = num3
							});
						}
						else
						{
							uri = current.GetUri(tilePositionX, tilePositionY, num);
						}
						tileImageLayerSources.Add(uri);
						num3++;
					}
				}
			}
		}
		private void RefreshInvalidTiles()
		{
			int num = (this.MinTileZoomLevel < 1) ? 1 : this.MinTileZoomLevel;
			for (int i = num; i < this.fakeNoTile.Length; i++)
			{
				List<MultiScaleQuadTileSource.TileInfo> list = this.fakeNoTile[i];
				using (List<MultiScaleQuadTileSource.TileInfo>.Enumerator enumerator = list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						MultiScaleQuadTileSource.TileInfo current = enumerator.get_Current();
						base.InvalidateTileLayer(i + this.zoomLevelDelta, current.X, current.Y, current.Layer);
					}
				}
				list.Clear();
			}
		}
	}
}
